package com.kifas.controller;

import java.util.ArrayList;
import java.util.List;

import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.UriInfo;
import javax.xml.bind.JAXBElement;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.kifas.entity.CustomerEntity;
import com.kifas.entity.JenisCustomerEntity;
import com.kifas.entity.SalesEntity;
import com.kifas.model.xml.CustomerModel;
import com.kifas.model.xml.JenisCustomerModel;
import com.kifas.model.xml.PersistStatus;
import com.kifas.model.xml.SalesModel;
import com.kifas.service.EntityServiceManager;

@Component
@Scope("prototype")
@Path("/Customers")
public class CustomerController {

	private static Logger log = Logger.getLogger(CustomerController.class);

	@Context
	UriInfo uriInfo;

	@Autowired
	private EntityServiceManager entityManager;

	@GET
	@Produces(MediaType.TEXT_XML)
	public List<CustomerModel> getCustomers() {
		List<CustomerEntity> listCustomersEntity = entityManager.getCustomers();
		List<CustomerModel> customers = new ArrayList<CustomerModel>();
		for (CustomerEntity customerEntity : listCustomersEntity) {
			SalesModel sales = new SalesModel(customerEntity.getSales().getIdSales(), customerEntity.getSales().getNama(), customerEntity.getSales().getType(),
					customerEntity.getCreatedBy());
			JenisCustomerModel j = new JenisCustomerModel(customerEntity.getJenisCustomer().getIdJenisCustomer(), customerEntity.getJenisCustomer()
					.getJenisCustomer(), customerEntity.getJenisCustomer().getCreatedBy());
			CustomerModel model = new CustomerModel(customerEntity.getIdCustomer(), customerEntity.getNama(), customerEntity.getAlamat1(),
					customerEntity.getAlamat2(), customerEntity.getNoTelp(), customerEntity.getEmail(), customerEntity.getContactPerson(), j,
					customerEntity.getAlamat3(), customerEntity.getNoTelp1(), customerEntity.getNoTelp2(), customerEntity.getContactPerson1(),
					customerEntity.getFax(), customerEntity.getNpwp(), customerEntity.getBank_acc(), customerEntity.getTgl_entry(), sales,
					customerEntity.getCreatedBy());
			customers.add(model);
		}
		return customers;
	}

	@Path("{id}")
	@GET
	public CustomerModel getCustomer(@PathParam("id") Long id) {
		CustomerEntity customerEntity = entityManager.getCustomer(id);
		SalesModel sales = new SalesModel(customerEntity.getSales().getIdSales(), customerEntity.getSales().getNama(), customerEntity.getSales().getType(),
				customerEntity.getSales().getCreatedBy());
		JenisCustomerModel jenisCustomer = new JenisCustomerModel(customerEntity.getJenisCustomer().getIdJenisCustomer(), customerEntity.getJenisCustomer()
				.getJenisCustomer(), customerEntity.getJenisCustomer().getCreatedBy());
		CustomerModel model = new CustomerModel(customerEntity.getIdCustomer(), customerEntity.getNama(), customerEntity.getAlamat1(),
				customerEntity.getAlamat2(), customerEntity.getNoTelp(), customerEntity.getEmail(), customerEntity.getContactPerson(), jenisCustomer,
				customerEntity.getAlamat3(), customerEntity.getNoTelp1(), customerEntity.getNoTelp2(), customerEntity.getContactPerson1(),
				customerEntity.getFax(), customerEntity.getNpwp(), customerEntity.getBank_acc(), customerEntity.getTgl_entry(), sales,
				customerEntity.getCreatedBy());
		return model;
	}

	@POST
	@Produces(MediaType.APPLICATION_XML)
	@Consumes(MediaType.APPLICATION_XML)
	public PersistStatus insert(JAXBElement<CustomerModel> xml) {
		CustomerModel model = xml.getValue();
		JenisCustomerEntity j = new JenisCustomerEntity(model.getJenisCustomer().getIdJenisCustomer(), model.getJenisCustomer().getJenisCustomer());
		SalesEntity salesEntity = new SalesEntity(model.getSales().getIdSales(), model.getSales().getNama(), model.getSales().getType(), model.getSales()
				.getCreatedBy());
		CustomerEntity customer = new CustomerEntity(null, model.getNama(), model.getAlamat1(), model.getAlamat2(), model.getNoTelp(), model.getEmail(),
				model.getContactPerson(), j, model.getAlamat3(), model.getNoTelp1(), model.getNoTelp2(), model.getContactPerson1(), model.getFax(),
				model.getNpwp(), model.getBank_acc(), model.getTgl_entry(), salesEntity, model.getCreatedBy());
		try {
			entityManager.persist(customer);
		} catch (Exception e) {
			log.error("can not persist customer entity", e);
			return new PersistStatus("CustomerModel", "failed", "can not persist customer");
		}
		return new PersistStatus("CustomerModel", "success", "success save customer data");
	}

	@PUT
	@Produces(MediaType.APPLICATION_XML)
	@Consumes(MediaType.APPLICATION_XML)
	public PersistStatus update(JAXBElement<CustomerModel> xml) {
		CustomerModel model = xml.getValue();
		CustomerEntity customerEntity = entityManager.getCustomer(model.getIdCustomer());
		JenisCustomerEntity jenisCustomer = entityManager.getJenisCustomer(model.getJenisCustomer().getIdJenisCustomer());
		if (customerEntity == null || jenisCustomer == null) {
			return new PersistStatus("CustomerModel", "failed", "customer not exist in database");
		}
		try {
			customerEntity.setAlamat1(model.getAlamat1());
			customerEntity.setAlamat2(model.getAlamat2());
			customerEntity.setContactPerson(model.getContactPerson());
			customerEntity.setEmail(model.getEmail());
			customerEntity.setJenisCustomer(jenisCustomer);
			customerEntity.setNama(model.getNama());
			customerEntity.setIdCustomer(model.getIdCustomer());

			entityManager.update(customerEntity);
		} catch (Exception e) {
			return new PersistStatus("CustomerModel", "failed", "can not persist customer");
		}
		return new PersistStatus("CustomerModel", "success", "success save data customer");
	}

}